﻿#include "statetool.h"

#define STATE_WHITE "color: rgba(255,255,255,0.1); font-size:17px;padding: 4px 0 0 0px;"
#define STATE_GREEN "color: rgba(0,255,0); font-size:17px;padding: 4px 0 0 0px;"
#define STATE_RED   "color: red; font-size:17px;padding: 4px 0 0 0px;"

TStateTool::TStateTool(QString title, int type, bool shadow, QWidget *parent) : QWidget(parent)
{
    this->twinkle = false;

    this->title = title;
    this->type = type;
    this->shadow = shadow;
    this->state = false;

    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
}

void TStateTool::InitWidget(){
    labTitle = new QLabel(this->title + ":");

    labValue = new QLabel("异常");
    labValue->setObjectName("white");

    labState = new QLabel(this->type == 0 ? QChar(0xe8e8) : QChar(0xe73b));
    //labState = new QLabel(QChar(0xe73b));
    labState->setStyleSheet(STATE_RED);

    if(shadow){
        QGraphicsDropShadowEffect *defaultShadow = new QGraphicsDropShadowEffect();
        defaultShadow->setBlurRadius(6.0);
        defaultShadow->setColor(QColor(0, 0, 0, 160));
        defaultShadow->setOffset(3.0);
        labState->setGraphicsEffect(defaultShadow);
    }

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(5);
    mainLayout->addWidget(labTitle);
    mainLayout->addWidget(labValue);
    mainLayout->addWidget(labState);
    this->setLayout(mainLayout);
}

void TStateTool::SetupConnections(){

}

void TStateTool::Initialize(){

}


void TStateTool::SetTwinkle(int time){
    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    timer->start(time);
}

void TStateTool::OnTimer(){
    if(!this->state){
        labState->setStyleSheet(twinkle ? STATE_RED : STATE_WHITE);
        twinkle = !twinkle;
    }else{
        labState->setStyleSheet(STATE_GREEN);
    }
}


void TStateTool::SetState(bool state){
    this->state = state;
    if(state){
        labValue->setText("正常");
        labState->setStyleSheet(STATE_GREEN);
    }else{
        labValue->setText("异常");
        labState->setStyleSheet(STATE_RED);
    }
}

void TStateTool::SetTitleHide(){
    labTitle->hide();
}
void TStateTool::SetValueHide(){
    labValue->hide();
}
